//
//  NavigationViewController.swift
//  QNN
//
//  Created by zhenyu on 17/3/30.
//  Copyright © 2017年 qianshengqian. All rights reserved.
//

import UIKit

class NavigationController: UINavigationController {
  
  var isNormal = true
  
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    
    navigationBar.shadowImage = UIImage()
    
    // TODO: status bar 白色
    navigationBar.setBackgroundImage(UIImage.imageWithColor(color: Color.theme), for: .default)
    navigationBar.barTintColor = UIColor.white
    navigationBar.tintColor = UIColor.white
    navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    navigationItem.hidesBackButton = true
  }
  
  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
  
  override func pushViewController(_ viewController: UIViewController, animated: Bool) {
    if viewControllers.count >= 1 {
      if viewController.navigationItem.leftBarButtonItem == nil {
        setViewControllerBackButton(viewController)
      }
      viewController.hidesBottomBarWhenPushed = true
    }
    super.pushViewController(viewController, animated: animated)
  }
  
  func setViewControllerBackButton(_ vc: UIViewController) {
    let backItem = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44)).then({ (b) in
      
      if isNormal {
        b.setImage(#imageLiteral(resourceName: "nav_back"), for: .normal)
        b.setImage(#imageLiteral(resourceName: "nav_back"), for: .highlighted)
      } else {
        b.setImage(#imageLiteral(resourceName: "nav_back_black"), for: .normal)
        b.setImage(#imageLiteral(resourceName: "nav_back_black"), for: .highlighted)
      }
      b.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 25)
      b.addTarget(vc, action: #selector(ViewController.popBack), for: .touchUpInside)
    })

    vc.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backItem)
  }
}
